package boundary;
import java.awt.*;
import control.*;
import javax.swing.*;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


/*******************************************************
*  Team name: FTPMP
*  Class involve: GameSettingWindow, GameSettingController, RoleAssigner
*  Version, Author, History of modification: 
*  Ver 1.1, Ada Ko, update err msg
*******************************************************/

/*----------------------------------------
class function: initialize game setting pages (eg lang/setting/role/stance)
class involved: mainFrame, GameSettingController, RoleStanceAssigner

----------------------------------------*/
public class GameSettingWindow extends JPanel implements ActionListener
{
	private int winMeth; 
	private int playerNo; 
	private int winSE;
	private GameSettingController controller;
	private RoleAssigner assigner;
	
	// var for GUI
	private CardLayout cl;
	private Container frame;
	private JPanel contentPane;
	//lang Panel
	private JButton btnEnglish;
	//setting panel
	private JButton settingNextBtn;
	
	//role panel
	private JButton roleShowBtn;
	private JButton roleStartBtn;
	private JLabel[] lblPlayer;
	private JLabel[] lblRole;
	//start panel
	private JButton GSStartBtn;
	private ButtonGroup playerNoBtn = new ButtonGroup();
	private JRadioButton[] playerNoOption;
	private JRadioButton[] winSEOption;
	private ButtonGroup winSEBtn = new ButtonGroup();
	private JPanel settingPanel;
	private  JPanel rolePanel;
	public JPanel startPanel;
	//private JPanel stancePanel;
	private JPanel langPanel;
	
	boolean btnEnglishClicked=false;
	private JButton[] roleOptions;
	private String[] roleType;
	private ButtonGroup winMethBtn = new ButtonGroup();
	private JRadioButton[] winMethOption = new JRadioButton[2];

	public GameSettingWindow (Container frame,final CardLayout cl,BoardEngine BE)
	{			
		//initialization data members
		roleType=new String[6];
		roleType[0]="Father";
		roleType[1]="Mother";
		roleType[2]="Grandpa";
		roleType[3]="Grandma";
		roleType[4]="Daughter";
		roleType[5]=" Son ";
		
		this.winMeth=1;
		this.winSE=0;
		this.playerNo=0;
		this.controller=new GameSettingController(this,BE);
		this.assigner= new RoleAssigner(BE);
		
		//initialization for GUI
		setBounds(0, 0, 1024, 768);
		this.contentPane=new JPanel();
		contentPane.setBorder(null);
		contentPane.setPreferredSize(new Dimension(1024, 768));
		add(contentPane);
		this.cl=new CardLayout(0, 0);
		contentPane.setLayout(this.cl);
		this.frame=frame;
		//initialize different panels
		this.setLang();
		this.setSetting();
		//this.setRole();
	
	}
	/*1.  choose lang page : setLang()
	  2.  choose setting: setSetting()
	  3.  choose role   : setRole()
	 */
	
	private void setLang()
	{
		langPanel = new JPanel();
		//LangPanel.setBounds(0,0,800,600);
		contentPane.add(langPanel,"LangPanel");
		
		cl.addLayoutComponent(langPanel, "LangPanel");

		JLabel EngTitle = new JLabel("Happy Family Game");
		EngTitle.setBounds(179, 153, 541, 70);
		EngTitle.setHorizontalAlignment(SwingConstants.CENTER);
		EngTitle.setFont(new Font("Arial", Font.PLAIN, 60));
		
		JLabel ChiTitle = new JLabel("\u958B\u5FC3\u5BB6\u5EAD\u6A02");
		ChiTitle.setBounds(353, 241, 300, 87);
		ChiTitle.setHorizontalAlignment(SwingConstants.CENTER);
		ChiTitle.setFont(new Font("Dialog", Font.PLAIN, 60));
		
		JLabel LangEngText = new JLabel("Choose a Language:");
		LangEngText.setBounds(255, 399, 420, 53);
		LangEngText.setHorizontalAlignment(SwingConstants.CENTER);
		LangEngText.setFont(new Font("Arial", Font.PLAIN, 45));
		
		JLabel LangChiText = new JLabel("\u8ACB\u9078\u64C7\u8A9E\u8A00:");
		LangChiText.setBounds(369, 481, 272, 65);
		LangChiText.setFont(new Font("Dialog", Font.PLAIN, 45));
		
		btnEnglish = new JButton("English");
		btnEnglish.setBounds(410, 584, 171, 62);
		btnEnglish.addActionListener(this);
	

		btnEnglish.setFont(new Font("Arial", Font.PLAIN, 30));
		langPanel.setLayout(null);
		langPanel.add(LangChiText);
		langPanel.add(ChiTitle);
		langPanel.add(EngTitle);
		langPanel.add(LangEngText);
		langPanel.add(btnEnglish);
	}

	private void setSetting()
	{
		settingPanel = new JPanel();
		contentPane.add(settingPanel,"SettingPanel");
		cl.addLayoutComponent(settingPanel, "SettingPanel");
		settingPanel.setLayout(null);
		
		initPlayerOption();
		initGSTitle();
		initWinCon();
		initWinMeth();
		initSettingBtn();
		ProcessSettingBtn();
	}
	
	private void initPlayerOption()
	{
		JLabel PlayerNoText = new JLabel("No of Players:");
		PlayerNoText.setBounds(160, 243, 248, 47);
		PlayerNoText.setHorizontalAlignment(SwingConstants.LEFT);
		PlayerNoText.setFont(new Font("Arial", Font.PLAIN, 40));
		this.playerNoOption=new JRadioButton[5];
		int x=160;
		for(int i=0;i<5;i++)
		{
			this.playerNoOption[i]=new JRadioButton(Integer.toString(i+2));
			this.playerNoOption[i].setFont(new Font("Arial", Font.PLAIN, 24));
			this.playerNoOption[i].setBounds(x, 290, 60, 50);
			this.playerNoBtn.add(this.playerNoOption[i]);
			settingPanel.add(playerNoOption[i]);
			x+=70;
		}
		settingPanel.add(PlayerNoText);
	}
	
	private void initGSTitle()
	{
		JLabel GSTitle = new JLabel("Game Setting:");
		GSTitle.setBounds(290, 45, 320, 59);
		GSTitle.setFont(new Font("Arial", Font.PLAIN, 50));
		settingPanel.add(GSTitle);
	}
	
	private void initWinCon()
	{
		JLabel WinConText = new JLabel("Winning condition:");
		WinConText.setBounds(160, 136, 318, 47);
		WinConText.setFont(new Font("Arial", Font.PLAIN, 40));
		settingPanel.add(WinConText);
		
		this.winSEOption=new JRadioButton[4];
		int x=160;
		for(int i=0;i<4;i++)
		{
			this.winSEOption[i]=new JRadioButton(Integer.toString((i+1)*10));
			this.winSEOption[i].setFont(new Font("Arial", Font.PLAIN, 24));
			this.winSEOption[i].setBounds(x, 187, 60, 50);
			this.winSEBtn.add(this.winSEOption[i]);
			settingPanel.add(winSEOption[i]);
			x+=70;
		}
	}
	
	private void initWinMeth()
	{
		JLabel WinMethText = new JLabel("Winning method:");
		WinMethText.setBounds(160, 358, 295, 47);
		WinMethText.setFont(new Font("Arial", Font.PLAIN, 40));
		this.winMethOption=new JRadioButton[2];
		winMethOption[0] = new JRadioButton("Collect enough SE together");
		winMethOption[0].setBounds(160, 460, 335, 37);
		winMethOption[0].setEnabled(true);
		
		winMethOption[1] = new JRadioButton("Be the first one to collect enough SE");
		winMethOption[1].setBounds(160, 423, 435, 37);
		
		for(int i=0;i<2;i++)
		{
			winMethBtn.add(winMethOption[i]);
			winMethOption[i].setFont(new Font("Arial", Font.PLAIN, 25));
			winMethOption[i].addActionListener(this);
			settingPanel.add(winMethOption[i]);
		}
		settingPanel.add(WinMethText);
	}
	
	private void initSettingBtn()
	{
		settingNextBtn = new JButton("Next");
		settingNextBtn.setBounds(466, 547, 150, 61);
		settingNextBtn.setFont(new Font("Arial", Font.PLAIN, 45));
		settingNextBtn.addActionListener(this);
		settingPanel.add(settingNextBtn);
	}
	
	private void ProcessSettingBtn()
	{
		//set WinSE
		for(int i=0;i<4;i++)
		{
			final int tempSE=10*(i+1);
			winSEOption[i].addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) 
				{
					if(isEnabled())
						winSE=tempSE;
				}
			});
		}

		for(int i=0;i<5;i++)
		{
			final int tempNo=2+i;
			
			playerNoOption[i].addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) 
				{
					if(isEnabled())
						playerNo=tempNo;
				}
			});
		}
	}
	
	
	private void setRole()
	{
		rolePanel = new JPanel();
		contentPane.add(rolePanel);
		cl.addLayoutComponent(rolePanel, "RolePanel");
		
		JLabel roleTitle = new JLabel("Select Role Here:");
		roleTitle.setBounds(73, 10, 286, 43);
		roleTitle.setFont(new Font("Arial Black", Font.PLAIN, 30));
		
		rolePanel.setLayout(null);
		rolePanel.add(roleTitle);
		
		initRoleBtn();
		initRoleOptions();
		initRoleLbls();
		
		setRoleList();
		
	}
	private void initRoleBtn()
	{
		roleShowBtn = new JButton("Show");
		roleShowBtn.setBounds(326, 668, 137, 51);
		roleShowBtn.setFont(new Font("Arial Black", Font.PLAIN, 30));
		roleShowBtn.addActionListener(this);
		
		roleStartBtn = new JButton("Start");
		roleStartBtn.setBounds(486, 668, 137, 51);
		roleStartBtn.setFont(new Font("Arial Black", Font.PLAIN, 30));
		roleStartBtn.addActionListener(this);
		
		rolePanel.add(roleShowBtn);
		rolePanel.add(roleStartBtn);
		
	}
	@SuppressWarnings("deprecation")
	private void initRoleOptions()
	{
		roleOptions=new JButton[6];
		ImageIcon icons=new ImageIcon("images\\Role_Unknown.gif");
		for(int j=0,k=0,y=97;j<2;j++,y+=260)
			for(int i=0, x=73;i<3;i++,x+=220,k++)
			{
				roleOptions[k]=new JButton(icons);
				roleOptions[k].setLayout(new BorderLayout());
				roleOptions[k].setLabel(this.roleType[k]);	
				roleOptions[k].setBounds(x, y, 170, 250);
				rolePanel.add(roleOptions[k]);
			}
	}
	private void initRoleLbls()
	{
		lblPlayer=new JLabel[6];
		lblRole=new JLabel[6];
		
		for(int i=1,x=200;i<7;i++,x+=60)
		{
			lblPlayer[i-1]=new JLabel("Player "+i+": ");
			lblPlayer[i-1].setFont(new Font("Arial", Font.PLAIN, 18));
			lblPlayer[i-1].setVisible(false);
			lblPlayer[i-1].setBounds(743, x, 80, 22);
			
			lblRole[i-1]= new JLabel("---");
			lblRole[i-1].setLabelFor(lblPlayer[i-1]);
			lblRole[i-1].setFont(new Font("Arial", Font.PLAIN, 18));
			lblRole[i-1].setVisible(false);
			lblRole[i-1].setBounds(864, x, 80, 22);
			
			rolePanel.add(lblPlayer[i-1]);
			rolePanel.add(lblRole[i-1]);
		}
	}
	// set role values in RoleStanceAssigner class on clicking the role btns
	private void setRoleList()
	{
		
		assigner.setPlayerNo(this.playerNo);	
		for(int i=0;i<this.playerNo;i++)
		{
			lblPlayer[i].setVisible(true);
			lblRole[i].setVisible(true);
		}	
		roleStartBtn.setEnabled(false);
		for(int i=0;i<6;i++)
		{
			@SuppressWarnings("deprecation")
			final String tempRole=roleOptions[i].getLabel();
			final JButton currentBtn=roleOptions[i];
			currentBtn.addActionListener(new ActionListener()
			{
				@Override
				public void actionPerformed(ActionEvent e) 
				{
					
					assigner.inputRole(tempRole);
					currentBtn.setEnabled(false);
					lblRole[assigner.getCount()-1].setText(tempRole);
					promptChooseRole();
				}
			});	
		}
	
	}
	
	private void showRoleOptions()
	{
		this.roleOptions[0].setIcon(new ImageIcon("images\\Role_Father.gif"));
		this.roleOptions[1].setIcon(new ImageIcon("images\\Role_Mother.gif"));
		this.roleOptions[2].setIcon(new ImageIcon("images\\Role_Grandpa.gif"));
		this.roleOptions[3].setIcon(new ImageIcon("images\\Role_Grandma.gif"));
		this.roleOptions[4].setIcon(new ImageIcon("images\\Role_Daughter.gif"));
		this.roleOptions[5].setIcon(new ImageIcon("images\\Role_Son.gif"));
	}
	
	private void promptChooseRole()
	{
		
		if(assigner.getCount()==this.playerNo)
		{
			JOptionPane.showMessageDialog(null,"All Players have chosen roles. Click Start to Continue.");
			for(int i=0;i<6;i++)
			{
				if(this.roleOptions[i].isEnabled())
					this.roleOptions[i].setEnabled(false);
				
			}
			roleStartBtn.setEnabled(true);
		}
		else
		{
			int currentPlayer=assigner.getCount()+1;
			JOptionPane.showMessageDialog(null,"Player "+currentPlayer+"'s turn to choose a role.");
		}
	}

	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource()==btnEnglish)
		{
			cl.show(contentPane, "SettingPanel");
		}
		else if(e.getSource()==this.winMethOption[0])
			this.winMeth=1;
		else if(e.getSource()==this.winMethOption[1])
			this.winMeth=2;
		else if(e.getSource()==this.settingNextBtn)
		{
			validateSettingOptions();
		}
		else if(e.getSource()==this.roleStartBtn)
		{
			//cl.show(contentPane, "StartPanel");
			assigner.assignRole();
			((CardLayout) frame.getLayout()).show(((JPanel) frame), "roundWindow");
		}
		else if(e.getSource()==this.roleShowBtn)
		{
			showRoleOptions();
			this.roleShowBtn.setEnabled(false);
		}
	}
	private void validateSettingOptions()
	{
		if( this.winSE!=0 && this.playerNo!=0)
		{
			setRole();
			System.out.println(playerNo+" "+winSE);
			cl.show(contentPane, "RolePanel");
			controller.process(playerNo,winSE,winMeth);
			this.promptChooseRole();
		}
		else if(this.winSE==0 && this.playerNo==0)
		{
			JOptionPane.showMessageDialog(null,"You have not selected player number and winning condition.");		
		}
		else if(this.playerNo==0)
		{
			JOptionPane.showMessageDialog(null,"You have not selected player number yet");
		}
		else if(this.winSE==0)
			JOptionPane.showMessageDialog(null,"You have not selected winning condition yet");
	}
	public int getPlayerNo() {
		return this.playerNo;
	}
	public JButton getSettingBtn()
	{
		return this.settingNextBtn;
	}
	public JButton[] getRoleOptions()
	{
		return this.roleOptions;
	}
	public JButton getRoleStartBtn() 
	{
		return this.roleStartBtn;
	}

	public JButton getBtnEnglish() 
	{
		return this.btnEnglish;
	}
	
	public JRadioButton[] getplayerNoBtn()
	{
		return this.playerNoOption;
	}
	
	public JRadioButton[] getwinConBtn()
	{
		return this.winSEOption;
	}
	public JRadioButton[] getwinMethBtn()
	{
		return this.winMethOption;
	}
}
